package org.sgx.gwteditors.testgallery.gallery.app;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.sgx.gwteditors.client.util.Util;
import org.sgx.gwteditors.testgallery.gallery.Array1DEditor2Test1;
import org.sgx.gwteditors.testgallery.gallery.Array1DEditorTest1;
import org.sgx.gwteditors.testgallery.gallery.BeanEditorTest1;
import org.sgx.gwteditors.testgallery.gallery.BeanEditorTest2;
import org.sgx.gwteditors.testgallery.gallery.BeanEditorTest3;
import org.sgx.gwteditors.testgallery.gallery.BeanEditorTestWithAnnotations;
import org.sgx.gwteditors.testgallery.gallery.ColorEditorTest;
import org.sgx.gwteditors.testgallery.gallery.EditorOfEditorTest1;
import org.sgx.gwteditors.testgallery.gallery.EditorsGalleryTest;
import org.sgx.gwteditors.testgallery.gallery.ErrorsTest1;
import org.sgx.gwteditors.testgallery.gallery.HTML5FileEditorTest1;
import org.sgx.gwteditors.testgallery.gallery.InRangeTest1;
import org.sgx.gwteditors.testgallery.gallery.IntegerEditor2Test;
import org.sgx.gwteditors.testgallery.gallery.PropertyHaverEditor1EventsTTest1;
import org.sgx.gwteditors.testgallery.gallery.PropertyHaverEditor1TTest1;
import org.sgx.gwteditors.testgallery.gallery.PropertyHaverTableEditor1Test1;
import org.sgx.gwteditors.testgallery.gallery.PropertyHaverTableEditor2Test1;
import org.sgx.gwteditors.testgallery.gallery.SelectionListTest;
import org.sgx.gwteditors.testgallery.gallery.Test;
import org.sgx.gwteditors.testgallery.gallery.simple.SimpleDateAndTimeEditorTest;
import org.sgx.gwteditors.testgallery.gallery.yui.YuiEditorsTest1;

import com.google.gwt.user.client.Window;
/**
 * 
 * @author sg
 *
 */
public class GalleryUtil {
	
private static GalleryUtil instance;

private GalleryUtil() {
}

public static GalleryUtil getInstance() {
	if (null == instance) {
		instance = new GalleryUtil();
	}
	return instance;
}

private LinkedList<Test> alltests=null;

public List<Test> getAllTests() {
	if(alltests==null) {
		alltests = new LinkedList<Test>();	
		alltests.add(new PropertyHaverEditor1TTest1());
		alltests.add(new PropertyHaverEditor1EventsTTest1());
		alltests.add(new Array1DEditorTest1());
		alltests.add(new BeanEditorTest1());
		alltests.add(new SelectionListTest());
		alltests.add(new ColorEditorTest());
		alltests.add(new BeanEditorTest2());
		alltests.add(new BeanEditorTest3());
		alltests.add(new ErrorsTest1());
		alltests.add(new InRangeTest1());
		alltests.add(new IntegerEditor2Test());
		alltests.add(new BeanEditorTestWithAnnotations());
		alltests.add(new Array1DEditor2Test1());
		alltests.add(new PropertyHaverTableEditor1Test1());
		alltests.add(new HTML5FileEditorTest1());
		alltests.add(new PropertyHaverTableEditor2Test1());
		alltests.add(new EditorsGalleryTest());
		alltests.add(new SimpleDateAndTimeEditorTest());				
		alltests.add(new YuiEditorsTest1());		
		alltests.add(new EditorOfEditorTest1());
		
	}
	return alltests;
}
public Test getTestByName(String name) {
	for(Test t : getAllTests()) {
		if(t.getName().equals(name))
			return t;
	}
	return null;
}

/**
 * check for a test=testName paraemter passed in the url and show that test if that is the case.
 */
public void checkUrl() {
	String url = Util.getCurrentAddressUrl(); 
//	System.out.println("URL: "+url);
	Map<String, String> urlParams = Util.parseUrlParams(url);
//	boolean found = false;
	final Gallery2 gal = Gallery2.getInstance();
	Test test = null;
	if (urlParams.containsKey("test")) {
		String paramTestName = urlParams.get("test"); 
		for (Test t : getAllTests()) {
			if (t.getName().equals(paramTestName)) {
//				found = true;
				gal.doRunTest(t);
				test=t;					
				break;
			}
		}
		if (test==null) {
			Window.alert("test " + urlParams.get("test") + " not found. ");
		}
		
		else if (urlParams.containsKey("showSources")) {
			gal.setCurrentTest(test); 
			gal.doShowCurrentTestSources(); 
		}
	}

}
}
